package com.founder.controller;



import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.founder.model.Client;
import com.founder.service.IClientServcie;
import com.founder.util.CommonUtil;
import com.github.pagehelper.Page;


@Controller
@RequestMapping("/client")
public class ClientController {
	@Autowired
	private IClientServcie clientServcie;

	@RequestMapping(value="client",method = RequestMethod.PATCH)
	@ResponseBody
	public Map<String, Object> pageClient(HttpServletRequest request) {
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");

		int pageNum = Integer.parseInt(page);
		int pageSize = Integer.parseInt(rows);

		String orgId = request.getParameter("orgId");
		String name = request.getParameter("name");
		String ip = request.getParameter("ip");

		Map<String, Object> map = new HashMap<String, Object>();
		Page<Client> pageClient = clientServcie.pageClient(pageNum, pageSize,
				"1", name, ip);
		map.put("rows", pageClient.getResult());
		map.put("total", pageClient.getTotal());
		map.put("pages", pageClient.getPages());
		return map;
	}

	@RequestMapping(value="/new",method = RequestMethod.PUT)
	@ResponseBody
	
	public Map<String, Object> addClient(@ModelAttribute("client") Client client) {
		Map<String, Object> map = new HashMap<String, Object>();
		client.setId(CommonUtil.uuid());
		client.setClientIp("1");
		int result = clientServcie.addClient(client);
		map.put("result", result);
		if (result == 0) {
			map.put("tip", "修改失败�?");
		} else {
			map.put("client", client);
		}
		return map;
	}

	@RequestMapping(value="/del",method = RequestMethod.DELETE)
	@ResponseBody
	public Map<String, Object> delClientById(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		Client c = new Client();
		c.setId(id);
		int result = clientServcie.delClientById(c);
		map.put("result", result);
		if (result == 0) {
			map.put("tip", "修改失败�?");
		}
		return map;
	}

	@RequestMapping(value="/update",method = RequestMethod.POST)
	@ResponseBody
	
	public Map<String, Object> modClient(@ModelAttribute("client") Client client) {
		Map<String, Object> map = new HashMap<String, Object>();
		int result = clientServcie.modClient(client);
		map.put("result", result);
		if (result == 0) {
			map.put("tip", "添加失败�?");
		} else {
			map.put("client", client);
		}
		return map;
	}

	@RequestMapping(value="/client",method = RequestMethod.POST)
	@ResponseBody
	
	public Map<String, Object> getClientById(HttpServletRequest request) {
		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();
		Client client = clientServcie.getClientById(id);
		map.put("client", client);
		return map;
	}
}
